from framework.base_page import BasePage
from selenium.webdriver.common.by import By
from framework.public_class import *


class HomePage(BasePage):
    """我的主页相关操作"""
    # 等待头像显示出来
    def wait_head_icon(self):
        return self.is_visible(locator=(By.XPATH, "//div[@class='thumb']"))

    # 等待底部第一页页码元素显示
    def wait_pagerbtn(self):
        return self.is_visible(locator=(By.CLASS_NAME, "acgn-active"))

    # 点击四个货币，钻石、金币、推荐票、月票
    def click_four_currency(self, num):
        """1--钻石、2--金币、3--推荐票、4--月票"""
        if num == 1:
            self.element_click(selector="xpath=>//a[@class='product-item-content' and @href='/uc/gold.html']")
        elif num == 2:
            self.element_click(selector="xpath=>//a[@class='product-item-content' and @href='/uc/coins.html']")
        elif num == 3:
            self.element_click(selector="xpath=>//a[@class='product-item-content' and @href='/uc/recommend.html']")
        elif num == 4:
            self.element_click(selector="xpath=>//a[@class='product-item-content' and @href='/uc/ticket.html']")
        else:
            raise Exception("num must be in 1-4!")

    # 点击名字旁的修改用户信息按钮
    def click_modify_user_info(self):
        self.element_click(selector="css=>[class='edit-user-info J_editUserInfo']")

    # 移动屏幕至 我的个人主页 显示在最上方
    def move_my_page_to_top(self):
        el = self.find_element(selector="class=>acgn-hd")
        self.move_to_element(el=el, location="top")

    # 找所有的编辑按钮并点击其中一个
    def click_edit_btns(self, num):
        edit_btn_list = self.find_elements(selector="css=>[class='modify-msg J_modifyMsg']")
        if num > len(edit_btn_list):
            raise Exception("num参数值大于编辑按钮的个数")
        self.element_click(element=edit_btn_list[num-1], log_msg="编辑按钮")

    # 移动保存按钮在最下方
    def move_save_btn_to_foot(self, num):
        if num == 1:
            save_el = self.find_element(selector="id=>J_savePersonalData")
        else:
            raise Exception("num must be in 1-7!")
        self.move_to_element(el=save_el, location="foot")

    # 输入需更改的信息
    def modify_user_info(self):
        # 更改昵称为当前时间的毫秒级时间戳
        time_now = str(round(time.time() * 1000))
        username_text = "旭%s" % time_now
        usersign_text = "哈哈哈这就是我的签名%s" % time_now
        self.input_text(selector="id=>J_username", text=username_text)
        # 更改性别
        self.element_click(selector="class=>ift-radio")
        # 更改个性签名
        self.input_text(selector="id=>J_signarea", text=usersign_text)
        ge_ren_zi_liao_div = self.find_element(selector="xpath=>//div[@class='modifybox personaldata']")  # 个人资料的div
        self.element_click(selector="id=>J_savePersonalData")  # 点击保存
        self.wait_page_reload(element=ge_ren_zi_liao_div)  # 以个人资料的div判定，等待页面刷新
        return self.toast_is_visible(locator=(By.ID, "J_showname"), text=username_text)

    # 等到上传头像的保存按钮显示出来
    def wait_uploadface_save(self):
        return self.is_visible(locator=(By.ID, "uploadFace"))


class ShuJiaPage(BasePage):
    """书架相关操作"""

    # 点击我的书架
    def click_shujia(self):
        self.element_click(selector="xpath=>//a[@href='/uc/bookrack.html']")

    # 等待我的收藏旁边的数字显示出来
    def wait_collect_num_is_visible(self):
        return self.is_visible(locator=(By.CLASS_NAME, "J_collectTotal"))

    # 没有阅读记录及订阅的漫画时，查找下方默认显示的头像文字
    def find_null_box(self):
        null_box_el = self.find_element(selector="class=>common-null-box")
        if null_box_el:
            return True
        else:
            return False

    # 点击编辑按钮
    def click_edit_btn(self):
        self.element_click(selector="class=>ift-edit")

    # 勾选我的收藏或者我的历史第一个漫画
    def select_comic(self, collect_or_history="collect"):
        # tab_active_el = self.find_element(selector="class=>tabs-item")
        # comic_el = self.find_element(selector="xpath=>//div[@class='tabs-item']/i", parent_element=tab_active_el)
        if collect_or_history == "collect":
            comic_el = self.find_element(selector="xpath=>//ul[@id = 'J_collectList']/li[1]/div[1]")
            self.element_click(element=comic_el, log_msg="我的收藏-漫画勾选")
        elif collect_or_history == "history":
            comic_el = self.find_element(selector="xpath=>//ul[@id = 'J_ucHistoryList']/li[1]/div[1]")
            self.element_click(element=comic_el, log_msg="我的历史-漫画勾选")
        else:
            raise Exception("collect_or_history must be collect or history")

    # 点删除按钮
    def click_del_btn(self, collect_or_history="collect"):
        del_btns = self.find_elements(selector="css=>[class='acgn-item item-delete J_delete isfocus']")
        if collect_or_history == "collect":
            self.element_click(element=del_btns[0], log_msg="我的收藏-删除按钮")
        elif collect_or_history == "history":
            self.element_click(element=del_btns[1], log_msg="我的历史-删除按钮")
        else:
            raise Exception("collect_or_history must be collect or history")

    # 我的收藏和阅读历史的数量
    def collect_history_num(self, item="collect"):
        if item == "collect":
            collect_num_el = self.find_element(selector="class=>J_collectTotal")
            collect_num = int(collect_num_el.text[1:-1])
            return [collect_num, collect_num_el]
        elif item == "history":
            history_num_el = self.find_element(selector="class=>J_historyTotal")
            history_num = int(history_num_el.text[1:-1])
            return [history_num, history_num_el]
        else:
            raise Exception("item must be collect or history!")

    # 点击删除后等待书架页面刷新
    def wait_shujia_page_reload(self, element):
        return self.wait_page_reload(element=element)

    # 点击 我的历史
    def click_my_history(self):
        self.element_click(selector="class=>J_historyTotal")


class ChargeCenterPage(BasePage):
    """充值中心相关操作"""

    # 进入充值中心
    def open_charge_center(self):
        self.element_click(selector="xpath=>//a[@href='pay.html']")

    # 等待充值列表显示出来
    def wait_charge_list_is_visible(self):
        return self.is_visible(locator=(By.CLASS_NAME, "acgn-item"))

    # 选择第一个金额
    def choose_first_price(self):
        self.element_click(selector="class=>acgn-item")

    # 点击去充值
    def click_gopay(self):
        self.element_click(selector="id=>J_gopay")

    # 等待二维码显示出来
    def wait_qrcode_is_visible(self):
        return self.is_visible(locator=(By.XPATH, "//div[@class='qrcode-img-area']"))


class MyBookList(BasePage):
    """我的书单相关操作"""

    # 支付宝充值页面返回个人中心
    def go_back(self):
        current_url = self.current_url()  # 获取当前url
        kanman_url = con.get_config(section="test_config", key="test_url")
        if kanman_url not in current_url:  # 当前url里没有kanman.com
            self.back()

    # 进入我的书单
    def open_my_book_list(self):
        book_list_el = self.find_element(selector="xpath=>//a[@href='/uc/booklist.html']")
        self.move_to_element(el=book_list_el, location="foot")
        self.element_click(element=book_list_el, log_msg="我的书单按钮")

    # 获取列表状态，空书单/有书单/网络异常
    def get_book_list_status(self):
        book_null_el = self.find_element(selector="id=>bookNull")  # 书单列表为空
        book_error_el = self.find_element(selector="id=>bookError")  # 获取书单列表失败，网络异常
        book_page_el = self.find_element(selector="id=>bookPage")  # 获取不为空的书单列表
        book_null_el_style = book_null_el.get_attribute("style")
        book_error_el_style = book_error_el.get_attribute("style")
        book_page_el_style = book_page_el.get_attribute("style")
        if "block" in book_error_el_style:  # 获取书单网络异常时，截图
            self.get_windows_img(error_info="获取书单网络异常")
        return {"book_null_el_style": 1 if "block" in book_null_el_style else 0,
                "book_error_el_style": 1 if "block" in book_error_el_style else 0,
                "book_page_el_style": 1 if "block" in book_page_el_style else 0}  # 1--当前显示的是此模块，0--当前显示不是此模块

    # 点击创建书单
    def click_create_book(self):
        self.element_click(selector="class=>ift-addBook")

    # 等待书单title显示出来
    def wait_book_name_is_visible(self, create_or_edt="create"):
        if create_or_edt == "create":
            return self.is_visible(locator=(By.CLASS_NAME, "label-title"), text="书单名")
        elif create_or_edt == "edt":
            title = con.get_config(section="user_center_config", key="usercenter_book_title")
            return self.is_visible(locator=(By.ID, "J_bookName"), text=title, text_type="value")
        else:
            raise Exception("create_or_edt must be create or edt")

    # 输入书单名和简介
    def input_book_name_and_desc(self, create_or_edt="create"):
        if create_or_edt == "create":
            title = con.get_config(section="user_center_config", key="usercenter_book_title")\
                    + str(round(time.time() * 1000))
            desc = con.get_config(section="user_center_config",
                                  key="usercenter_book_desc") + str(round(time.time() * 1000))
        elif create_or_edt == "edt":
            title = con.get_config(section="user_center_config", key="usercenter_edt_book_title")\
                    + str(round(time.time() * 1000))
            desc = con.get_config(section="user_center_config",
                                  key="usercenter_edt_book_desc") + str(round(time.time() * 1000))
        else:
            raise Exception("create_or_edt must be create or edt")
        self.input_text(selector="id=>J_bookName", text=title)
        self.input_text(selector="J_bookDesc", text=desc)
        return title

    # 点击发布书单按钮
    def click_submit_book(self):
        self.element_click(selector="id=>J_submitBook")

    # 搜索漫画
    def search_comic(self, create_or_edt="create"):
        """传入create_or_edt值不同，取不同的搜索值"""
        if create_or_edt == "create":
            search_text = con.get_config(section="user_center_config", key="create_book_search_text")
        elif create_or_edt == "edt":
            search_text = con.get_config(section="user_center_config", key="edt_book_search_text")
        else:
            raise Exception("create_or_edt must be create or edt")
        self.input_text(selector="J_searchInput", text=search_text)  # 输入文字
        self.element_click(selector="id=>J_searchBookBtn")  # 点击搜索按钮

    # 等待搜索结果显示出来
    def wait_search_result_is_visible(self):
        return self.is_visible(locator=(By.XPATH, "//ul[@id='J_searchBd']/li"))  # 等待搜索结果中的第一个漫画显示出来

    # 点击搜索到的第一个漫画
    def click_search_comic(self):
        self.element_click(selector="class=>book-listItem-comic")

    # 等待漫画添加成功
    def wait_comic_add_success(self):
        return self.toast_is_visible(locator=(By.CLASS_NAME, "layer-custom-text"), text="已添加")

    # 等待我的书单页面第一个书单加载出来
    def wait_reload_book_list_page_success(self):
        return self.is_visible(locator=(By.XPATH, "//li[@class='myBook-listItem J_bookList']"))

    # 以个人中心第一个书单的书单名与创建的相等来判断书单创建成功
    def create_book_success(self, create_title):
        first_book_title = self.find_element(selector="class=>bookItemName").text
        return True if create_title in first_book_title else False

    # 点击编辑书单
    def click_edt(self):
        self.element_click(selector="xpath=>//i[@class='ift-edit J_editBook']")

    # 点击删除书单
    def click_del_book(self):
        self.element_click(selector="xpath=>//i[@class='ift-del J_deleteBook']")

    # 等待确定弹窗弹出
    def wait_confirm_window(self):
        return self.is_visible(locator=(By.CLASS_NAME, "book-list-content"))

    # 点击确定
    def click_confirm_btn(self):
        self.element_click(selector="xpath=>//div[@class='book-btn-item confirm J_confirm']")

    # 等待删除书单成功的toast弹出
    def wait_del_book_success_toast(self):
        return self.toast_is_visible(locator=(By.CLASS_NAME, "layer-custom-text"), text="删除书单成功！")
